package com.ygxsk.carhome.ui.member;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.gyf.immersionbar.ImmersionBar;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.tencent.mmkv.MMKV;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.constant.ConstantSign;
import com.ygxsk.carhome.response.PointListItemBean;
import com.ygxsk.carhome.ui.ErrorActivity;
import com.ygxsk.carhome.ui.adapter.PointListItemAdapter;
import com.ygxsk.carhome.ui.leads.LeadDetailActivity;
import com.ygxsk.carhome.views.MyLinearLayoutManager;
import com.ygxsk.carhome.views.picker.NomalCustomBean;
import com.ygxsk.carhome.views.spinner.NiceSpinnersk;

import java.util.ArrayList;

import butterknife.BindView;

/*
 * @Author:itchenqi175@163.com
 * @Time:2023-08-02  09:01
 * @Description:积分明细列表
 */
public class PointListActivity extends ErrorActivity {

    @BindView(R.id.refreshlayoutinpointlist)
    SmartRefreshLayout refreshLayoutInPointList;

    @BindView(R.id.recyclerviewinpointlist)
    RecyclerView recyclerViewInPointList;

    //选择类型
    @BindView(R.id.nice_spinner_pointtype)
    NiceSpinnersk nice_spinner_pointtype;
    //选择来源
    @BindView(R.id.nice_spinner_point_starttime)
    NiceSpinnersk nice_spinner_point_starttime;
    //选择状态
    @BindView(R.id.nice_spinner_point_endtime)
    NiceSpinnersk nice_spinner_point_endtime;


    //筛选条件的 汽车的类型；
    private ArrayList<NomalCustomBean> dataListCarType = new ArrayList<>();
    //客户的来源
    private ArrayList<NomalCustomBean> dataListFromWhere = new ArrayList<>();
    //该客户的分配状态
    private ArrayList<NomalCustomBean> dataListStateItem = new ArrayList<>();

    private ArrayList<PointListItemBean> mDataResources = new ArrayList<>();

    //积分管理的适配器
    private PointListItemAdapter pointListItemAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mActivityInstance = this;

        if (dataListCarType == null) {
            dataListCarType = new ArrayList<>();
        }

        if (dataListFromWhere == null) {
            dataListFromWhere = new ArrayList<>();
        }

        if (dataListStateItem == null) {
            dataListStateItem = new ArrayList<>();
        }

        initloading();

        initViews();

        //其他的类似 监听器的，提交一个客户

        //监听单个客户内容
        //列表之内需要向外部广播数据
//        if (!EventBus.getDefault().isRegistered(this)) {
//            EventBus.getDefault().register(this);
//        }

    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();

        ImmersionBar.with(this)//
                .titleBar(R.id.toolbarygxskout)
                .fitsSystemWindows(true)//解决状态栏和布局重叠问题，任选其一，默认为false，当为true时一定要指定statusBarColor()，不然状态栏为透明色，还有一些重载方法
                //.statusBarColor(R.color.colorPrimary)//
                //.navigationBarColor(R.color.colorPrimary)//
                //.keyboardEnable(true)
                .init();

    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_point_list;
    }

    @Override
    protected void initToolBar(ToolbarHelper toolbarHelper) {

        toolbarHelper.setTitle(getString(R.string.leadscustomer));
        toolbarHelper.setVersionName(true, AppUtils.getAppVersionName());
        toolbarHelper.setLayoutClose(false, "", null);
        toolbarHelper.setLayoutLeft(true, R.drawable.backicon, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //搜索客户信息，优先关闭软键盘
                KeyboardUtils.hideSoftInput(mActivityInstance);

                methodBack();

            }
        });

        //添加潜在客户的按钮

        toolbarHelper.setLayoutRight(false, 1, "", null);

//        toolbarHelper.setLayoutRight(true, 0, "添加", new ClickUtils.OnDebouncingClickListener() {
//            @Override
//            public void onDebouncingClick(View v) {
//
//                //快速双加的限制，
//                // TODO: 2023-07-11 16:18 提交潜在客户的信息
//                // TODO: 2023-07-11 14:24 跳转到提交潜在客户页面：
//                Bundle extra_bundle = new Bundle();
//                ActivityUtils.startActivity(extra_bundle, LeadAddSubmitActivity.class);
//
//            }
//        });

    }

    @Override
    protected void onClickNodata(View v) {

        gainDatas();

    }

    @Override
    protected void initViews() {

        PointListItemBean pointListItemBean = new PointListItemBean();
        pointListItemBean.setPointTypeInfo("积分签到");
        pointListItemBean.setPointTime("2023-10-15 05:20:10");
        pointListItemBean.setPointInCreaseOrDeCrease(1);//是积分的增加
        pointListItemBean.setPointAmount(10);

        PointListItemBean pointListItemBean1 = new PointListItemBean();
        pointListItemBean1.setPointTypeInfo("积分兑换");
        pointListItemBean1.setPointTime("2023-10-15 05:20:10");
        pointListItemBean1.setPointInCreaseOrDeCrease(0);//是积分的增加
        pointListItemBean1.setPointAmount(10);

        PointListItemBean pointListItemBean2 = new PointListItemBean();
        pointListItemBean2.setPointTypeInfo("积分签到");
        pointListItemBean2.setPointTime("2023-10-15 05:20:10");
        pointListItemBean2.setPointInCreaseOrDeCrease(1);//是积分的增加
        pointListItemBean2.setPointAmount(100);

        mDataResources.add(pointListItemBean);
        mDataResources.add(pointListItemBean1);
        mDataResources.add(pointListItemBean2);


        NomalCustomBean nomalCustomBean = new NomalCustomBean();
        nomalCustomBean.setDictCode("1");
        nomalCustomBean.setDictLabel("已分配");

        NomalCustomBean nomalCustomBean1 = new NomalCustomBean();
        nomalCustomBean1.setDictCode("2");
        nomalCustomBean1.setDictLabel("已确认");

        NomalCustomBean nomalCustomBean2 = new NomalCustomBean();
        nomalCustomBean2.setDictCode("3");
        nomalCustomBean2.setDictLabel("战败");

        NomalCustomBean nomalCustomBean3 = new NomalCustomBean();
        nomalCustomBean3.setDictCode("4");
        nomalCustomBean3.setDictLabel("成功");

        dataListStateItem.add(nomalCustomBean);
        dataListStateItem.add(nomalCustomBean1);
        dataListStateItem.add(nomalCustomBean2);
        dataListStateItem.add(nomalCustomBean3);

        MMKV.defaultMMKV().encode(ConstantSign.MMKV_JSON_NOMAL_CUSTOM_JSON, JSON.toJSONString(dataListStateItem));

//        method_spinnser_stateItem();

        //----------------------------------------------------------------------------------
        //布局的适配器填充，下拉刷新等等；
        recyclerViewInPointList.setLayoutManager(new MyLinearLayoutManager(this));

        pointListItemAdapter = new PointListItemAdapter(R.layout.pointslistadapter, mDataResources);
        recyclerViewInPointList.setAdapter(pointListItemAdapter);

        pointListItemAdapter.notifyDataSetChanged();

        pointListItemAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {

                // TODO: 2023-07-13 15:07 跳转到这个客户的详情页面
                Bundle extra_bundle = new Bundle();

//                extra_bundle.putParcelable(ExtraKey.EXTRA_RUNMANGROUPMANAGEBEAN, runManGroupManageBeanItem);

                ActivityUtils.startActivity(extra_bundle, LeadDetailActivity.class);

            }
        });

        refreshLayoutInPointList.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

                refreshLayout.getLayout().post(new Runnable() {
                    @Override
                    public void run() {

                        if (mDataResources != null && !mDataResources.isEmpty()) {
                            mDataResources.clear();
                        }

                        // TODO: 2023-07-13 15:11 待处理

//                        getServerDatas();

                        refreshLayout.finishRefresh();
                    }
                });

            }

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

                refreshLayout.getLayout().post(new Runnable() {
                    @Override
                    public void run() {

                        //最后一条数据的id信息；
                        //getServerDatas(repairIdPass);

                        refreshLayout.finishLoadMore();

                    }
                });
            }
        });


    }

    @Override
    protected void gainDatas() {

        //

    }

    @Override
    protected void methodBack() {
        if (ActivityUtils.isActivityAlive(this)) {
            ActivityUtils.finishActivity(this);
        }
    }
}